<?php



final class CMS_Xsl_Transform {
	
    private $transformation = "";
    
	private function process($xml, $xsl, $param, $asString) {
        if($asString) {
            $DomXml = DOMDocument::loadXML($xml)
			or die('Le fichier xml est indisponible ou comporte des erreurs !');
        }
        else {
            $DomXml = DOMDocument::load($xml)
			or die('Le fichier xml est indisponible ou comporte des erreurs !');
        }

		$DomXsl = DOMDocument::load($xsl)
		or die('Le fichier xsl est indisponible ou comporte des erreurs !');

		$process = new XSLTProcessor();
		
		$process->registerPHPFunctions();
		$process->importStyleSheet($DomXsl);
		foreach($param as $key => $value){
			$process->setParameter("", $key, htmlspecialchars($value));
		}
		
		
        $this->transformation = $process->transformToXML($DomXml);

	}

	public function __construct($xml, $xsl, $param, $asString = false, $setHeaders = true) {
        
		if($setHeaders) {
			header('Content-type: text/html; charset=utf-8');
			header('vary: user-agent');
			ini_set('arg_separator.output', '&amp;');
			//ob_start('ob_gzhandler');
		}
		
		return is_string($xml) && is_string($xsl) && is_array($param) ?
			$this->process($xml, $xsl, $param, $asString):
			false;
   }
   
   public function __toString(){
        return $this->transformation;
   }
   

}

?>